﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class PrePurchaseRequestRejectDAL : IDALPrePurchaseRequestReject
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PrePurchaseRequestRejectDAL()
        {

        }

        public void Insert(PrePurchaseRequestRejectInfo info)
        {

        }

        public void Insert(int PprSeq,DateTime RejectDate,string RejectReason,int RejectBySeq)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                int RejectNo = this.GetNextRejectNo(PprSeq, dbManager);
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@icm_ppr_seq", PprSeq);
                dbManager.AddParameters(1, "@icm_ppr_rej_no", RejectNo);
                dbManager.AddParameters(2, "@icm_ppr_rej_date", RejectDate.Date);
                dbManager.AddParameters(3, "@icm_ppr_rej_reason", RejectReason);
                dbManager.AddParameters(4, "@rejected_by", RejectBySeq);
                dbManager.AddParameters(5, "@rejected_when", DateTime.Now);

                strSQL = "INSERT INTO icm_pre_purchase_request_reject"
                    + " (icm_ppr_seq,icm_ppr_rej_no,icm_ppr_rej_date,icm_ppr_rej_reason,rejected_by,rejected_when)"
                    + " VALUES(@icm_ppr_seq"
                    + ", @icm_ppr_rej_no"
                    + ", @icm_ppr_rej_date"
                    + ", @icm_ppr_rej_reason"
                    + ", @rejected_by"
                    + ", @rejected_when"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                PrePurchaseRequestDAL dalPpr = new PrePurchaseRequestDAL();
                dalPpr.UpdateStatus(PprSeq, SystemInfo._RejectStatus, RejectBySeq, dbManager);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        private int GetNextRejectNo(int PprSeq, IDBManager dbManagerTrans)
        {
            try
            {
                dbManagerTrans.CreateParameters(1);
                dbManagerTrans.AddParameters(0, "@icm_ppr_seq", PprSeq);
                string strSql;
                strSql = "select coalesce((select coalesce(max( icm_ppr_rej_no) + 1, 1)" +
                         " from icm_pre_purchase_request_reject " +
                         " where icm_ppr_seq = @icm_ppr_seq" +
                         " group by icm_ppr_seq), 1)";
                DataSet ds;
                ds = dbManagerTrans.ExecuteDataSet(CommandType.Text, strSql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
                }
                throw new Exception("no data");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(PrePurchaseRequestRejectInfo info)
        {

        }

        public void Delete(PrePurchaseRequestRejectInfo info)
        {

        }

        public IList<PrePurchaseRequestRejectInfo> GetHistoryReturned(int seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<PrePurchaseRequestRejectInfo> infoList = new List<PrePurchaseRequestRejectInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_ppr_seq", seq);

                strSQL = "select r.icm_ppr_rej_seq,r.icm_ppr_rej_no" +
                            ", icm_ppr_rej_date" +
                            ", concat(p.prefix_name, e.first_name, ' ' ,e.last_name) as reject_emp" +
                            ", icm_ppr_rej_reason" +
                        " from icm_pre_purchase_request_reject r" +
                            " inner join hrm_ps_employees e on r.rejected_by = e.emp_seq" +
                            " inner join mas_name_prefixes p on e.name_prefix_seq = p.name_prefix_seq" +
                            " where icm_ppr_seq = @icm_ppr_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        PrePurchaseRequestRejectInfo info = new PrePurchaseRequestRejectInfo();
                        HrmEmployeeInfo InfoEmp= new HrmEmployeeInfo();
                        info.Seq = Convert.ToInt32(dr[i]["icm_ppr_rej_seq"]);
                        info.Reject_no = (Convert.IsDBNull(dr[i]["icm_ppr_rej_no"]) ? null : (Nullable<int>)(dr[i]["icm_ppr_rej_no"]));
                        info.Date = (Convert.IsDBNull(dr[i]["icm_ppr_rej_date"]) ? null : (Nullable<DateTime>)(dr[i]["icm_ppr_rej_date"]));

                        InfoEmp.Name = dr[i]["reject_emp"].ToString();
                        info.InfoEmp = InfoEmp;

                        info.Reason = dr[i]["icm_ppr_rej_reason"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
